package zen2d.example.lol;

import zen2d.*;
import zen2d.SwingWindow;
import zen2d.util.AssertUtil;

import java.awt.*;
import java.awt.image.BufferedImage;


public class Dema extends Sprite {

    Direction direction;
    int speed = 5;

    int hp = 100;

    Animation walk_R;
    Animation walk_L;
    Animation walk_U;
    Animation walk_D;

    Animation xf;

    Animation current;

    Animation attack_L;

    Animation death_L;

    public Dema(int x, int y, int width, int height) {
        super(x, y, width, height);

        BufferedImage[][] bufferedImages = AssertUtil.split(AssertUtil.getImage("/image/lol/dema/dema-walk.png"),8,8);

        walk_R = new Animation();
        walk_R.setDuration(100);
        walk_R.setKeyFrames(bufferedImages[6]);

        walk_L = new Animation();
        walk_L.setDuration(100);
        walk_L.setKeyFrames(bufferedImages[2]);

        walk_U = new Animation();
        walk_U.setDuration(100);
        walk_U.setKeyFrames(bufferedImages[4]);

        walk_D = new Animation();
        walk_D.setDuration(100);
        walk_D.setKeyFrames(bufferedImages[0]);

        xf = new Animation();
        xf.setDuration(30);
        xf.setKeyFrames(AssertUtil.parseOneDimensionalArray(AssertUtil.split(AssertUtil.getImage("image/lol/dema/dema-xf.png"),2,4)));
//        xf.setKeyFrames(AssertUtil.split(AssertUtil.getImage("/assert/image/lol/manzi/manzi-xf.png"),4,3)[0]);
        xf.setPlayMode(Animation.PlayMode.NORMAL);
        current = walk_R;
        direction = Direction.S;


        BufferedImage [][] bufferedImages1 = AssertUtil.split(AssertUtil.getImage("image/lol/dema/dema-attack-1.png"),6,4);
        attack_L = new Animation();
        attack_L.setDuration(100);
        attack_L.setKeyFrames( AssertUtil.concatAll(bufferedImages1[4],bufferedImages1[5]));

        BufferedImage [][] bufferedImages2 = AssertUtil.split(AssertUtil.getImage("image/lol/dema/dema-death-1.png"),8,4);
        death_L = new Animation();
        death_L.setDuration(200);
        death_L.setPlayMode(Animation.PlayMode.NORMAL);
        death_L.setKeyFrames(AssertUtil.concatAll(bufferedImages2[4],bufferedImages2[5]));
    }

    @Override
    protected void afterEndLive() {

    }

    @Override
    protected void update() {

        switch (direction){
            case L:{
                x -= speed;
            }break;
            case R:{
                x+=speed;
            }break;
            case U:{
                y -= speed;
            }break;
            case D:{
                y+= speed;
            }break;
            case S:{

            }break;
        }

        Attack attack = Zen2d.getCollisionWithClass(this, Attack.class);
        if (null != attack) {

            if (attack.isSameHorizontal(this)){
                this.hp -= 10;
                attack.endLive();
            }
        }
        if (hp <= 0){
            current = death_L;
            Zen2d.addTask(2000, () -> direction = Direction.D);
        }

    }

    @Override
    protected void draw(GraphicsContext graphicsContext) {

         graphicsContext.drawImage(current.getKeyFrame(),x,y,width,height);

        graphicsContext.setColor(Color.PINK);
        graphicsContext.drawRect(x,y,width,height,false);

        graphicsContext.drawString(String.valueOf(hp),x+width/2,y-20,30);

    }

    @Override
    protected void up() {
        direction = Direction.U;
        current = walk_U;
    }

    @Override
    protected void down() {
        direction = Direction.D;
        current = walk_D;
    }

    @Override
    protected void left() {
        direction = Direction.L;
        current = walk_L;
    }

    @Override
    protected void right() {
        direction = Direction.R;
        current = walk_R;
    }

    @Override
    protected void space() {
        direction = Direction.S;
        xf.reset();
        current = attack_L;
    }
}
